/* 
* File:   RepositoryForm.h
* Author: Wardell Bagby
*      
*/
#pragma once
#include "Repository.h"
namespace Repository {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO;
	using namespace Project3;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//

		};
	private: System::Windows::Forms::Button^  loadEmail2;
	public: 

	public: 
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  helpToolStripMenuItem;
	private: System::Windows::Forms::CheckBox^  checkBox1;
			 Project3::Repository^ r;

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  loadEmail;
	private: System::Windows::Forms::Button^  buildOutputFile;
	protected: 

	protected: 

	private: System::Windows::Forms::Button^  setOutputFile;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->loadEmail = (gcnew System::Windows::Forms::Button());
			this->buildOutputFile = (gcnew System::Windows::Forms::Button());
			this->setOutputFile = (gcnew System::Windows::Forms::Button());
			this->loadEmail2 = (gcnew System::Windows::Forms::Button());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// loadEmail
			// 
			this->loadEmail->Enabled = false;
			this->loadEmail->Location = System::Drawing::Point(12, 56);
			this->loadEmail->Name = L"loadEmail";
			this->loadEmail->Size = System::Drawing::Size(139, 23);
			this->loadEmail->TabIndex = 0;
			this->loadEmail->Text = L"Load Email";
			this->loadEmail->UseVisualStyleBackColor = true;
			this->loadEmail->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// buildOutputFile
			// 
			this->buildOutputFile->Enabled = false;
			this->buildOutputFile->Location = System::Drawing::Point(11, 85);
			this->buildOutputFile->Name = L"buildOutputFile";
			this->buildOutputFile->Size = System::Drawing::Size(267, 23);
			this->buildOutputFile->TabIndex = 1;
			this->buildOutputFile->Text = L"Build Output File";
			this->buildOutputFile->UseVisualStyleBackColor = true;
			this->buildOutputFile->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// setOutputFile
			// 
			this->setOutputFile->Location = System::Drawing::Point(12, 27);
			this->setOutputFile->Name = L"setOutputFile";
			this->setOutputFile->Size = System::Drawing::Size(268, 23);
			this->setOutputFile->TabIndex = 2;
			this->setOutputFile->Text = L"Set Output File";
			this->setOutputFile->UseVisualStyleBackColor = true;
			this->setOutputFile->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// loadEmail2
			// 
			this->loadEmail2->Enabled = false;
			this->loadEmail2->Location = System::Drawing::Point(158, 57);
			this->loadEmail2->Name = L"loadEmail2";
			this->loadEmail2->Size = System::Drawing::Size(120, 23);
			this->loadEmail2->TabIndex = 3;
			this->loadEmail2->Text = L"Load Email (Alt)";
			this->loadEmail2->UseVisualStyleBackColor = true;
			this->loadEmail2->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->helpToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(289, 24);
			this->menuStrip1->TabIndex = 4;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// helpToolStripMenuItem
			// 
			this->helpToolStripMenuItem->Name = L"helpToolStripMenuItem";
			this->helpToolStripMenuItem->Size = System::Drawing::Size(40, 20);
			this->helpToolStripMenuItem->Text = L"Help";
			this->helpToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::helpToolStripMenuItem_Click);
			// 
			// checkBox1
			// 
			this->checkBox1->AutoSize = true;
			this->checkBox1->Location = System::Drawing::Point(12, 114);
			this->checkBox1->Name = L"checkBox1";
			this->checkBox1->Size = System::Drawing::Size(92, 17);
			this->checkBox1->TabIndex = 5;
			this->checkBox1->Text = L"Email Is Spam";
			this->checkBox1->UseVisualStyleBackColor = true;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(289, 138);
			this->Controls->Add(this->checkBox1);
			this->Controls->Add(this->loadEmail2);
			this->Controls->Add(this->setOutputFile);
			this->Controls->Add(this->buildOutputFile);
			this->Controls->Add(this->loadEmail);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->Text = L"Repository Creator";
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
		/**Loads the email assuming that the email doesn't start with Spam or Ham, so uses the checkbox to say whether it is or not. **/
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 Stream^ myStream;
				 OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
				 openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
				 openFileDialog1->FilterIndex = 2;
				 openFileDialog1->RestoreDirectory = true;
				 if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
				 {
					 if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
					 {
						 try
						 {
							 r->setCurrentEmail((FileStream^)myStream,checkBox1->Checked);
							 r->createEntries();
							 buildOutputFile->Enabled = true;

						 }
						 catch(Exception^ e)
						 {

						 }			 
						 myStream->Close();
					 }
					 else
					 {
						 //Insert code to see tell user to load valid Email File.
					 }
				 }
			 }
			 /**Builds the output file into the file specified by the user**/
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			r->buildOutputFile();
			buildOutputFile->Enabled = false;

				 
			 }
			 /**Sets the output file for writing the SpamFilter file into.**/
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
				 r = gcnew Project3::Repository();
				 Stream^ myStream;
				 SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
				 saveFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
				 saveFileDialog1->FilterIndex = 2;
				 saveFileDialog1->RestoreDirectory = true;
				 if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
				 {
					 if ( (myStream = saveFileDialog1->OpenFile()) != nullptr )
					 {
						 try
						 {
							 myStream->Close();
							 r->setOutputFile(File::Open(saveFileDialog1->FileName,FileMode::OpenOrCreate));
							 loadEmail->Enabled = true;
							 loadEmail2->Enabled = true;
						 }
						 catch(Exception^ e)
						 {
							 Console::WriteLine(e->StackTrace);
						 }			 
						 myStream->Close();
					 }
					 else
					 {
						 
					 }
				 }

			 }
			 /**Shows the Help dialog.**/
	private: System::Void helpToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 MessageBox::Show("Use \"Set Output File\" for setting the file that the entries will be written to.\nUse \"Load Email\" to load an Email that uses the checkbox to decide if its Spam or not.\nUse \"Load Email (Alt)\" for loading emails where the first line is true or spam and false or ham.\nUse \"Build Output File\" to create the actual file.");
			 }
			 /**Loads an email assuming that the first line in the email says whether it's Spam or Ham**/
	private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
				 OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
				 openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
				 openFileDialog1->FilterIndex = 2;
				 openFileDialog1->RestoreDirectory = true;
				 openFileDialog1->Multiselect = true;
				 array<String^>^ names;
				 if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
				 {
					 names = openFileDialog1->FileNames;
					 for(int i=0; i<names->Length; i++)
					 {
						 try
						 {
							 r->setCurrentEmail(File::OpenRead(names[i]));
							 r->createEntries();
 							 buildOutputFile->Enabled = true;
						 }
						 catch(Exception^ e)
						 {

						 }			 
					 }
				 }
			 };
	};
}

